<?php
/**

 */

namespace app\api\behavior;


use think\Response;
use think\facade\Request;
use app\api\model\Admin;
class CORS
{
	protected $adminToken;
    public function appInit()
    {

       header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept , identityCheck ,Token , adminToken");
        header('Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE,TRACE, OPTIONS, PATCH');

        if (Request()->method() == 'OPTIONS') {
            exit;
        }
        $this->identityCheck();
    }

    private function identityCheck(){

			$headers = Request()->header();
          
            if(isset($headers['identitycheck'])){
            	if(isset($headers['admintoken']) && $headers['identitycheck'] == '5REDouidJpej6cWS'){
				$this->adminToken = $headers['admintoken'];
				$this->adminLoginState();
			}else{
				return_json(40001 , '我的接口是谁都能请求的吗？' , null);
				exit;
			}
        }
			
	}

	//检查后台管理登录状态，状态码117代表后台管理登录失效
		private function adminLoginState(){
			 $data = Request::post();
            if(isset($data['username'])&&isset($data['password'])){
            	$model=new Admin();
            	$model->login($data);
            }
			$admin = db('admin')->where('token' , $this->adminToken)->find();
			if($admin == null){
				return_json(117 , '登录状态失效' , null);
				exit;
			}
		}

	
		
}